package framework.multimedia;

import framework.FRDController;
import framework.FRDModel;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 *
 * @author Lukáš Marek
 */
public class FRDMultimediaController extends FRDController implements ActionListener, MouseListener
{
	protected FRDMultimedia multimediaPanel;
	protected FRDMultimediaModel model;
	
	protected int showingItems = 6;
	protected FRDMultimediaItem items[];
	protected int page = 1;
	protected int count = 0;
	
	public FRDMultimediaController(FRDMultimedia panel, FRDModel model)
	{
		this.multimediaPanel = panel;
		this.model = (FRDMultimediaModel) model;
		initMultimediaItems();
		initListeners();
	}
	
	/**
	 * Inicializace vsech panelu
	 */
	protected void initMultimediaItems()
	{
		items = new FRDMultimediaItem[showingItems];
		for (int i = 0; i < showingItems; i++) //podle poctu showingItems inicializuje jednotlive panely pro zobrazovani dat
		{
			items[i] = new FRDMultimediaItem();
		}
		multimediaPanel.jItemPanel.add(items[0]);
		multimediaPanel.jItemPanel1.add(items[1]);
		multimediaPanel.jItemPanel2.add(items[2]);
		multimediaPanel.jItemPanel3.add(items[3]);
		multimediaPanel.jItemPanel4.add(items[4]);
		multimediaPanel.jItemPanel5.add(items[5]);
	}

	/**
	 * inicializace listeneru
	 */
	private void initListeners()
	{
		multimediaPanel.pagerButtonLeft.addActionListener(this);
		multimediaPanel.pagerButtonRight.addActionListener(this);
		multimediaPanel.addButton.addActionListener(this);
		multimediaPanel.searchButton.addActionListener(this);
		for (int i = 0; i < showingItems; i++)
		{
			items[i].addMouseListener(this);
		}
	}

	/**
	 * zpracovani kliknuti na nektere tlacitko
	 * @param e event
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{
		JButton button = (JButton) e.getSource();
		switch (button.getActionCommand())
		{
			case "pagerButtonLeft":
				if (page > 1)
				{
					page--;
					loadItems();
					multimediaPanel.repaint();
				}
				break;
			case "pagerButtonRight":
				if (getPages() > page)
				{
					page++;
					loadItems();
					multimediaPanel.repaint();
				}
				break;
			case "searchButton":
				page = 1;
				loadItems();
				multimediaPanel.repaint();
				break;
			default:
				System.err.println(button.getActionCommand());
				break;
		}
	}
	
	
	@Override
	protected LinkedList<String> setParameters()
	{
		LinkedList<String> ret = new LinkedList<>();
		ret.add(String.valueOf(page));
		ret.add(String.valueOf(showingItems));
		ret.add(multimediaPanel.nameTextField.getText());
		return ret;
	}
	
	/**
	 * Nacte z modelu data z databaze, data musi by ulozeny v seznamu (LinkedList) polozek typu FRDMultimediaDBItem
	 */
	@Override
	public void loadItems()
	{
		LinkedList<FRDMultimediaDBItem> DBItems = null;
		if (model != null)
		{
			LinkedList<String> params = setParameters();
			DBItems = model.getData(params);
			count = model.getDataCount(params);
			multimediaPanel.setPagerLabel(page + "/" + getPages());
		}
		if (DBItems != null)
		{
			for (int i = 0; i < DBItems.size(); i++)
			{
				Image im;
				ImageIcon imTmp;
				int width = items[i].getWidth();
				int height = items[i].getHeight();
				int x = 0;
				int y = 0;
				if ((DBItems.get(i).image.getIconHeight() / (double)DBItems.get(i).image.getIconWidth()) < 0.875)
					im = DBItems.get(i).image.getImage().getScaledInstance(width, -1, java.awt.Image.SCALE_SMOOTH);
				else
					im = DBItems.get(i).image.getImage().getScaledInstance(-1, height, java.awt.Image.SCALE_SMOOTH);
				
				//centrovani obrazku
				imTmp = new ImageIcon(im);
				if (imTmp.getIconHeight() < height)
					y = (height - imTmp.getIconHeight()) / 2;
				else if (imTmp.getIconWidth() < width)
					x = (width - imTmp.getIconWidth()) / 2;
				
				//prevod na BufferedImage
				BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
				image.getGraphics().drawImage(im, x, y, null);
				items[i].setImage(image);
				items[i].setId(DBItems.get(i).id);
				items[i].name.setText(DBItems.get(i).name);
			}
			if (DBItems.size() < showingItems)
				for (int i = DBItems.size(); i < showingItems; i++)
					items[i].setImage(null);
		}
	}

	/**
	 * Vraci pocet stranek
	 * @return pages
	 */
	protected int getPages()
	{
		int pages = count / showingItems;
		if (count % showingItems > 0) pages++;
		if (pages == 0) pages = 1;
		return pages;
	} 

	/**
	 * Akce pri kliknuti mysi, nic se nedeje, ale v komentari je kod ktery jednoznacne identifikuje prvek
	 * na ktery se kliknulo. Jednotlive tlacitka se zpracovavaji v actionPerformed
	 * @param e 
	 */
	@Override
	public void mouseClicked(MouseEvent e)
	{
//		FRDMultimediaItem item;
//		if (e.getSource() instanceof JButton)
//			item = (FRDMultimediaItem) ((JButton)e.getSource()).getParent();
//		else
//			item = (FRDMultimediaItem) e.getSource();
	}

	@Override
	public void mousePressed(MouseEvent e){}

	@Override
	public void mouseReleased(MouseEvent e){}

	@Override
	public void mouseEntered(MouseEvent e){}

	@Override
	public void mouseExited(MouseEvent e){}
}
